package org.jeecg.modules.exam.service.impl;

import org.jeecg.modules.exam.entity.Course;
import org.jeecg.modules.exam.entity.CourseClass;
import org.jeecg.modules.exam.mapper.CourseClassMapper;
import org.jeecg.modules.exam.mapper.CourseMapper;
import org.jeecg.modules.exam.service.ICourseService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 课程表
 * @Author: jeecg-boot
 * @Date:   2021-03-07
 * @Version: V1.0
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

	@Autowired
	private CourseMapper courseMapper;
	@Autowired
	private CourseClassMapper courseClassMapper;
	
	@Override
	@Transactional
	public void saveMain(Course course, List<CourseClass> courseClassList) {
		courseMapper.insert(course);
		if(courseClassList!=null && courseClassList.size()>0) {
			for(CourseClass entity:courseClassList) {
				//外键设置
				entity.setCourseId(course.getId());
				courseClassMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Course course,List<CourseClass> courseClassList) {
		courseMapper.updateById(course);
		
		//1.先删除子表数据
		courseClassMapper.deleteByMainId(course.getId());
		
		//2.子表数据重新插入
		if(courseClassList!=null && courseClassList.size()>0) {
			for(CourseClass entity:courseClassList) {
				//外键设置
				entity.setCourseId(course.getId());
				courseClassMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		courseClassMapper.deleteByMainId(id);
		courseMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			courseClassMapper.deleteByMainId(id.toString());
			courseMapper.deleteById(id);
		}
	}
	
}
